package com.pinyougou.manager.controller;

import com.pinyougou.pojo.TbGoods;
import com.pinyougou.sellergoods.service.GoodsService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * controller
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

  @Autowired private GoodsService goodsService;

  /**
   * 返回全部列表
   *
   * @return
   */
  @RequestMapping("/findAll")
  public List<TbGoods> findAll() {
    return goodsService.findAll();
  }

  @RequestMapping("/hello")
  public String SayHello() {
    return goodsService.SayHelloWorld();
  }

  /**
   * 返回全部列表
   *
   * @return
   */
  @RequestMapping("/findPage")
  public PageResult findPage(int page, int rows) {
    return goodsService.findPage(page, rows);
  }

  /**
   * 增加
   *
   * @param goods
   * @return
   */
  @RequestMapping("/add")
  public Result add(@RequestBody TbGoods goods) {
    try {
      goodsService.add(goods);
      return new Result(true, "增加成功");
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, "增加失败");
    }
  }

  /**
   * 修改
   *
   * @param goods
   * @return
   */
  @RequestMapping("/update")
  public Result update(@RequestBody TbGoods goods) {
    try {
      goodsService.update(goods);
      return new Result(true, "修改成功");
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, "修改失败");
    }
  }

  /**
   * 获取实体
   *
   * @param id
   * @return
   */
  @RequestMapping("/findOne")
  public TbGoods findOne(Long id) {
    return goodsService.findOne(id);
  }

  /**
   * 批量删除
   *
   * @param ids
   * @return
   */
  @RequestMapping("/delete")
  public Result delete(Long[] ids) {
    try {
      goodsService.delete(ids);
      return new Result(true, "删除成功");
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, "删除失败");
    }
  }

  /**
   * 查询+分页
   *
   * @param brand dd
   * @param page d
   * @param rows d
   * @return
   */
  @RequestMapping("/search")
  public PageResult search(@RequestBody TbGoods goods, int page, int rows) {
    return goodsService.findPage(goods, page, rows);
  }
}
